package com.example.community.add;

import com.example.community.dao.ComtyDao;
import com.example.community.dao.FloorDao;
import com.example.community.model.Comty;
import com.example.community.model.Floor;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/floorAddServlet")
public class FloorAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ComtyDao comtyDao = new ComtyDao();
        List<Comty> comties = comtyDao.fandByAll();
        req.setAttribute("comties",comties);
        req.getRequestDispatcher("/WEB-INF/houseManager/floorAdd.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String fname = req.getParameter("fname");
            String ctno = req.getParameter("ctno");

            Floor floor = new Floor();
            floor.setfName(fname);
            floor.setCtNo(Integer.parseInt(ctno));

            FloorDao floorDao = new FloorDao();
            Integer insert = floorDao.insert(floor);
            if (insert == 1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }catch (Exception e){
            resp.getWriter().write("false");
        }

    }
}
